package com.yong.android.framework.view;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Author:yong
 * Date:2017-12-21
 */

public class StatusLayout extends FrameLayout {
    private View mContentView;
    private List<View> viewList;
    private ViewHolder currentHolder;
    private Adapter mAdapter;
    private int mStatus;
    private boolean isShowContentView = false;
    private SparseArray<ViewHolder> mCacheViewHolder;

    public StatusLayout(@NonNull Context context) {
        super(context);
    }

    public StatusLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public StatusLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setContentView(View contentView) {
        setContentView(contentView, false);
    }

    public void setContentView(View contentView, boolean isVisible) {
        mContentView = contentView;
        switch (contentView.getVisibility()) {
            case View.VISIBLE:
                if (!isVisible) {
                    contentView.setVisibility(GONE);
                }
                break;
            default:
                if (isVisible) {
                    contentView.setVisibility(VISIBLE);
                }

                break;
        }
        isShowContentView = isVisible;
    }

    public void setAdapter(Adapter adapter) {
        if (mAdapter != null) {
            mAdapter.onDetachedFromStatusLayout(this);
        }
        this.mAdapter = adapter;
        if (mAdapter != null) {
            mAdapter.onAttachFromStatusLayout(this);
        }
    }

    public Adapter getAdapter() {
        return mAdapter;
    }

    public void addOtherView(View... views) {
        if (views == null || views.length == 0) {
            return;
        }
        if (viewList == null) {
            viewList = new ArrayList<>(views.length);
            for (int i = 0; i < views.length; i++) {
                viewList.add(views[i]);
            }
        }
    }

    @SuppressWarnings("unchecked")
    public void showContentView() {
        if (mContentView == null) {
            throw new IllegalStateException("statusLayout hasn't content view");
        }
        if (!isShowContentView) {
            if (currentHolder != null) {
                removeView(currentHolder.itemView);
                mAdapter.onViewDetachedFromWindow(currentHolder);
                currentHolder = null;
            }
            mContentView.setVisibility(VISIBLE);
            isShowContentView = true;
        }

    }

    private void hideContentView() {
        if (isShowContentView) {
            mContentView.setVisibility(GONE);
            isShowContentView = false;
        }
    }

    private ViewHolder getViewHolder(int status) {
        if (mCacheViewHolder == null) {
            return mAdapter.onCreateViewHolder(this, status);
        } else {
            ViewHolder holder = mCacheViewHolder.get(status);
            if (holder == null) {
                return mAdapter.onCreateViewHolder(this, status);
            } else {
                return holder;
            }
        }
    }

    @SuppressWarnings("unchecked")
    public void notifyStatus(int status) {
        if (mAdapter == null) {
            throw new IllegalStateException("adapter is null");
        }
        if (mStatus == status) {
            return;
        }
        mStatus = status;

        hideContentView();

        if (currentHolder != null) {
            removeView(currentHolder.itemView);
            mAdapter.onViewDetachedFromWindow(currentHolder);
        }

        currentHolder = mAdapter.onCreateViewHolder(this, status);

        if (currentHolder == null) {
            return;
        }
        mAdapter.onBindViewHolder(currentHolder);
        addView(currentHolder.itemView, 0);
        mAdapter.onViewAttachedToWindow(currentHolder);
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (mAdapter != null && currentHolder != null) {
            mAdapter.onViewAttachedToWindow(currentHolder);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mAdapter != null && currentHolder != null) {
            mAdapter.onViewDetachedFromWindow(currentHolder);
        }
    }

    public static abstract class Adapter<VH extends ViewHolder> {
        public abstract VH onCreateViewHolder(ViewGroup parent, int status);

        public abstract void onBindViewHolder(ViewHolder holder);

        public void onAttachFromStatusLayout(StatusLayout statusLayout) {

        }

        public void onDetachedFromStatusLayout(StatusLayout statusLayout) {

        }


        public void onViewAttachedToWindow(VH holder) {

        }

        public void onViewDetachedFromWindow(VH holder) {

        }
    }


    public static class ViewHolder {
        public View itemView;
        public boolean isCache;

        public ViewHolder(View itemView) {
            this.itemView = itemView;
        }

        public void setCache(boolean cache) {
            isCache = cache;
        }

        @SuppressWarnings("unchecked")
        public <V extends View> V findViewById(@IdRes int id) {
            return (V) itemView.findViewById(id);
        }

    }
}
